package com.vast.admin.controller.cietac.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vast.admin.annotation.PassToken;
import com.vast.admin.api.CietacApi;
import com.vast.admin.controller.cietac.entity.*;
import com.vast.admin.entity.Company;
import com.vast.admin.service.impl.CompanyService;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * App相关接口 前端控制器
 * </p>
 *
 * @author zhaohao
 * @since 2018-10-24
 */
@RestController
@Api(value = "贸仲相关的api", tags = { "贸仲服务模块" })
@RequestMapping("/cietac")
public class CietacController {

    @Autowired
    private CietacApi cietacApi;

    @Autowired
    private CompanyService companyService;

    @ApiOperation(value = "获取短信验证码", notes = "获取短信验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mobile", value = "手机号码", dataType = "String",  required = true)
    })
    @GetMapping("/getSmsCode")
    @PassToken
    public Result<Object> getSmsCode(String mobile) {
        if(StrUtil.isNotEmpty(mobile)){
            return cietacApi.sendSms(mobile);
        }else{
            return new Result(MessageCode.ERROR, "手机号码不能为空");
        }

    }

    @ApiOperation(value = "注册", notes = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "registInfoEntity", value = "用户类型（1个人2企业）", dataType = "RegistInfoEntity",  required = true),
    })
    @PostMapping("/register")
    @PassToken
    public Result<String> register(RegistInfoEntity registInfoEntity) {
        if(registInfoEntity != null && StrUtil.isNotEmpty(registInfoEntity.getMobile())){
            Result<String> result = cietacApi.register(registInfoEntity);
            if(result.getCode() == 0 || result.getCode() == 100015){
                //更新是否在贸仲注册状态
                QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
                companyQueryWrapper.eq(Company.COL_LEGAL_PERSON_MOBILE,registInfoEntity.getMobile());
                Company company = companyService.getOne(companyQueryWrapper);
                if(company != null){
                    company.setRegisterStatus("1");
                    companyService.updateById(company);
                }
            }
            return result;
        }else{
            return new Result(MessageCode.ERROR, "手机号码不能为空");
        }
    }

    @ApiOperation(value = "获取邮箱验证码", notes = "获取邮箱验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
            @ApiImplicitParam(paramType = "query", name = "receiveEmail", value = "邮箱", dataType = "String",  required = true)
    })
    @GetMapping("/getEmailCode")
    @PassToken
    public Result<String> getEmailCode(@RequestHeader String User,String receiveEmail) {
        if(StrUtil.isNotEmpty(receiveEmail)){
            return cietacApi.sendEmail(User,receiveEmail);
        }else{
            return new Result(MessageCode.ERROR, "邮箱不能为空");
        }
    }

    @ApiOperation(value = "绑定邮箱", notes = "绑定邮箱")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
            @ApiImplicitParam(paramType = "body", name = "emailEntity", value = "用户类型（1个人2企业）", dataType = "EmailBindEntity",  required = true),
    })
    @PostMapping("/bindEmail")
    @PassToken
    public Result<String> bindEmail(@RequestHeader String User, EmailBindEntity emailEntity) {
        if(emailEntity != null){
            return cietacApi.bindEmail(User,emailEntity);
        }else{
            return new Result(MessageCode.ERROR, "邮箱不能为空");
        }
    }

    @ApiOperation(value = "获取实名认证二维码", notes = "获取实名认证二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
    })
    @GetMapping("/realNameQRCode")
    @PassToken
    public Result<String> realNameQRCode(@RequestHeader String User) {
       return cietacApi.realNameQRCode(User);
    }


    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
    })
    @GetMapping("/getUserInfo")
    @PassToken
    public Result<Object> getUserInfo(@RequestHeader String User) {
        return cietacApi.info(User);
    }


    @ApiOperation(value = "获取实人认证token", notes = "获取实人认证token")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
    })
    @GetMapping("/getRealNameToken")
    @PassToken
    public Result<Object> getRealNameToken(@RequestHeader String User) {
        return cietacApi.realNameToken(User);
    }

    @ApiOperation(value = "获取认证状态", notes = "获取认证状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
    })
    @GetMapping("/identificationStatus")
    @PassToken
    public Result<Object> identificationStatus(@RequestHeader String User) {
        Result<Object> result = cietacApi.identificationStatus(User);
        if(result.getData().equals("")){
            result.setData(new Object());
        }
        return result;
    }

    @ApiOperation(value = "上传签名图片", notes = "上传签名图片")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
            @ApiImplicitParam(paramType = "body", name = "signEntity", dataType = "SignPictureMultipartFileParamEntity",  required = true),
    })
    @PostMapping("/uploadSign")
    @PassToken
    public Result<Object> uploadSign(@RequestHeader String User, SignPictureMultipartFileParamEntity signEntity) {
        return cietacApi.uploadSign(User,signEntity);
    }


    @ApiOperation(value = "上传企业信息", notes = "上传企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
            @ApiImplicitParam(paramType = "body", name = "companyEntity", dataType = "CompanyInfoMultipartFileParamEntity",  required = true),
    })
    @PostMapping("/uploadCompanyInfo")
    @PassToken
    public Result<Object> uploadCompanyInfo(@RequestHeader String User, CompanyInfoMultipartFileParamEntity companyEntity) {
        return cietacApi.uploadCompanyEntity(User,companyEntity);
    }

    @ApiOperation(value = "获取地区编码", notes = "获取地区编码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
    })
    @GetMapping("/getRegionId")
    @PassToken
    public Result<Object> getRegionId(@RequestHeader String User) {
        return cietacApi.getRegionId(User);
    }


    @ApiOperation(value = "获取企业管理员信息", notes = "获取企业管理员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
    })
    @GetMapping(value = "/getCompanyAdminInfo")
    @PassToken
    public Result<Object> getCompanyAdminInfo(@RequestHeader("User")String user){
        Result<Object> result = cietacApi.getCompanyAdminInfo(user);
        if(result.getData().equals("")){
            result.setData(new Object());
        }
        return result;
    };


    @ApiOperation(value = "添加企业管理员", notes = "添加企业管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "User", dataType = "String",  required = true),
            @ApiImplicitParam(paramType = "body", name = "companyAdminEntity", dataType = "CompanyAdminInfoMultipartFileParamEntity",  required = true),
    })
    @PostMapping(value = "/addCompanyAdmin")
    @PassToken
    Result<Object> addCompanyAdmin(@RequestHeader("User")String user, CompanyAdminInfoMultipartFileParamEntity companyAdminEntity){
        return cietacApi.addCompanyAdmin(user,companyAdminEntity);
    };


}
